//
//  BindPhoneViewController.m
//  KiklinkApp
//
//  Created by LF on 14/10/29.
//  Copyright (c) 2014年 Kiklink. All rights reserved.
//

#import "BindPhoneViewController.h"
#import "API.h"
#import "LoginNavigationViewController.h"


@interface BindPhoneViewController()

@property (weak, nonatomic) IBOutlet UITextField *phoneNumberTextField;
@property (weak, nonatomic) IBOutlet UITextField *verificationCodeTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UITextField *repeatPasswordTextField;


@property (strong, nonatomic) IBOutlet UIView *backgroundView;
@property (weak, nonatomic) IBOutlet UILabel *helloLable;

@end

Callback exitCallback;
NSDictionary *userData;

@implementation BindPhoneViewController

- (void)viewDidLoad {
	[super viewDidLoad];
	// Do any additional setup after loading the view.
	
	LoginNavigationViewController *navi = (LoginNavigationViewController*) self.navigationController;
	exitCallback = [navi loginCallback];
	userData = [navi userData];
	
	UITapGestureRecognizer *backgroundTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeyboard)];
	[self.backgroundView addGestureRecognizer:backgroundTapRecognizer];
}

- (void)viewWillAppear:(BOOL)animated
{
	if([userData objectForKey:KEY_DISPLAY_NAME] != nil)
	{
		self.helloLable.text = [[userData objectForKey:KEY_DISPLAY_NAME] stringByAppendingString:@"，你好！"];
	}
}

-(void)closeKeyboard
{
	[self.phoneNumberTextField resignFirstResponder];
	[self.verificationCodeTextField resignFirstResponder];
//	[self.passwordTextField resignFirstResponder];
//	[self.repeatPasswordTextField resignFirstResponder];
}

- (IBAction)omClickGetVerifiactionCode:(id)sender {
	
	if (! [AccountManager isPhoneNumberValid:self.phoneNumberTextField.text]) {
		showAlert(@"手机号格式不正确");
		return;
	}
	
	[AccountManager sendVerificationCodeTo:self.phoneNumberTextField.text];
}

- (IBAction)onClickGo:(id)sender {
	if (! [AccountManager isVerificationCodeValid:self.verificationCodeTextField.text]) {
		showAlert(@"验证码不正确");
		return;
	}
	
//	if (! [AccountManager isPasswordValid:self.passwordTextField.text]) {
//		showAlert(@"密码过短");
//		return;
//	}
//	
//	if (! [self.passwordTextField.text isEqualToString:self.repeatPasswordTextField.text]) {
//		showAlert(@"密码不一致");
//		return;
//	}
	
	NSString *displayName = [userData objectForKey:KEY_DISPLAY_NAME];
	if(displayName == nil)
	{
		displayName = self.phoneNumberTextField.text;
	}
	
	[API bindPhoneNumber:self.phoneNumberTextField.text callback:^(int stateCode, NSString *word, id content) {
		
		if(stateCode == FAILURE_RETURN_ERRER_CODE)
		{
			showAlert(word);
			return;
		}
		else if(stateCode < 0)
		{
			showAlert(@"绑定失败");
			return;
		}
		else
		{
			showAlert(@"绑定成功");
		}
	}];
	
	//	[API registKiklinkWithPhoneNumber:self.phoneNumberTextField.text verificationCode:self.verificationCodeTextField.text displayName:displayName password:self.passwordTextField.text Callback:^(int stateCode, NSString *word, id content) {
	//
	//		if(stateCode == FAILURE_RETURN_ERRER_CODE)
	//		{
	//			showAlert(word);
	//			return;
	//		}
	//		else if(stateCode < 0)
	//		{
	//			showAlert(@"绑定失败");
	//			return;
	//		}
	//		else
	//		{
	//			[AccountManager firstLoginKiklinkWithUsername:self.phoneNumberTextField.text password:self.passwordTextField.text callback:^(int stateCode, NSString *word, id content) {
	//				if(stateCode == FAILURE_RETURN_ERRER_CODE)
	//				{
	//					showAlert(word);
	//					return;
	//				}
	//				else if(stateCode < 0)
	//				{
	//					showAlert(@"绑定失败");	//登录失败也对用户概括为验证失败
	//					return;
	//				}
	//				else
	//				{
	//					showAlert(@"绑定成功");
	//
	//					//确认关联成功之后才写入用户信息。用户名密码交由login自动记录
	//					[AccountManager saveAccountInfo:userData];
	//
	//					[self dismissViewControllerAnimated:YES completion:nil];
	//					exitCallback(stateCode, word, content);
	//				}
	//			}];
	//		}
	//	}];
}

- (IBAction)onClickCancelButton:(id)sender {
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"若不绑定手机，您将无法使用第三方账号登录开练网" message:nil delegate:self cancelButtonTitle:@"继续绑定" otherButtonTitles:@"确认退出", nil ];
	[alert show];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
	KLLogWithFormat(self, @"alertViewButton: %@", @(buttonIndex).stringValue);
	if(buttonIndex == 1)
	{
		[self dismissViewControllerAnimated:YES completion:nil];
		exitCallback(FAILURE, @"用户取消操作", nil);
	}
}

@end
